package com.arethusa;

import simpleorm.core.SConnection;
import simpleorm.core.SDataSource;
import simpleorm.core.SException;
import simpleorm.core.SLog;

public class Olowus {

	static LangebaanDB.SystemProperties systemProperties = new LangebaanDB.SystemProperties();

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
		SLog.slog.level=0;
		initializeDB();
		SConnection.begin(); 
		LangebaanWedstrijdFrame lbw = new LangebaanWedstrijdFrame();
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( hasBegun ) SConnection.commit(); 
		lbw.setLocationRelativeTo(null);
		lbw.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

	private static void initializeDB() {
		String dburl = systemProperties.getProperty("database.url");
		if (dburl == null)
			throw new SException.Error("database.url property not found");
		dburl = dburl.trim()+"/"+systemProperties.getProperty("database.name");
		String dbUserName = systemProperties.getProperty("database.user", "sa");
		String dbPassword = systemProperties.getProperty("database.password", "");
		String dbDriver = systemProperties.getProperty("database.driver", "org.hsqldb.jdbcDriver");
		try {
			Class.forName(dbDriver);
		} catch (Exception ex) {
			throw new SException.JDBC("Loading " + dbDriver, ex);
		}
		
		SDataSource ds = new SDataSource(dburl, dbUserName, dbPassword);
		SConnection.attach(ds, "LBCon");
	}
}
